package ui.panel;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import bean.Adress;


/**
 * Frame for Editing / Creating an Adress
 * @author Martin Schophaus
 *
 */
@SuppressWarnings("serial")
public class AdressPanel extends JPanel {
	
	private class AdressActionListener implements ActionListener {

		private AdressPanel frame;
		
		public AdressActionListener(AdressPanel frame) {
			this.frame = frame;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			this.frame.submit();
		}
	}
	
	private JTextField form_id 				= new JTextField();
	private JTextField form_city 			= new JTextField();
	private JTextField form_postalcd 		= new JTextField();
	private JTextField form_street 			= new JTextField();
	private JTextField form_tel 			= new JTextField();
	private JTextField form_tel_mobile 		= new JTextField();
	private JTextField form_tel_fax 		= new JTextField();
	private JTextField form_email 			= new JTextField();
	private JButton form_submit				= new JButton("Submit");
	
	private Adress adress;
	private JDialog dialog;
	
	
	public AdressPanel (String title, Adress adress, JDialog dialog) {
	
		this.dialog = dialog;
		this.adress = adress;
		
		this.form_city.setText(adress.getCity());
		this.form_email.setText(adress.getEmail());
		this.form_id.setText(Integer.toString(adress.getId()));
		this.form_postalcd.setText(adress.getPostal_cd());
		this.form_street.setText(adress.getStreet());
		this.form_tel.setText(adress.getTel());
		this.form_tel_fax.setText(adress.getTel_fax());
		this.form_tel_mobile.setText(adress.getTel_mobile());
		this.form_id.setEditable(false);
		
		this.setLayout(new GridLayout(9, 2));
		this.add(new JLabel("Adress ID"));
		this.add(this.form_id);
		this.add(new JLabel("Stadt"));
		this.add(this.form_city);
		this.add(new JLabel("Postleitzahl"));
		this.add(this.form_postalcd);
		this.add(new JLabel("Strasse"));
		this.add(this.form_street);
		this.add(new JLabel("Tel"));
		this.add(this.form_tel);
		this.add(new JLabel("Mobil"));
		this.add(this.form_tel_mobile);
		this.add(new JLabel("Fax"));
		this.add(this.form_tel_fax);
		this.add(new JLabel("E-Mail"));
		this.add(this.form_email);
		this.add(this.form_submit);
		
		this.form_submit.addActionListener(new AdressActionListener(this));
		this.setVisible(true);
	}
	
	/**
	 * Sets the data to the text fields of the customer maintenance panel
	 */
	private void submit(){
		this.adress.setCity(this.form_city.getText());
		this.adress.setEmail(this.form_email.getText());
		this.adress.setPostal_cd(this.form_postalcd.getText());
		this.adress.setStreet(this.form_street.getText());
		this.adress.setTel(this.form_tel.getText());
		this.adress.setTel_fax(this.form_tel_fax.getText());
		this.adress.setTel_mobile(this.form_tel_mobile.getText());
		
		this.dialog.dispose();
	}
}
